//
//  SpecialNewsController.swift
//  网易新闻
//
//  Created by wl on 15/11/18.
//  Copyright © 2015年 wl. All rights reserved.
//  专题新闻界面控制器
/***************************************************
*  如果您发现任何BUG,或者有更好的建议或者意见，请您的指教。
*邮箱:wxl19950606@163.com.感谢您的支持
***************************************************/
import UIKit

class SpecialNewsController: UIViewController, SpecialNewsHeardViewDelegate {

    
//========================================================
// MARK: - 一些属性
//========================================================
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var titleLabel: UILabel!
    var type: String?
    var topicsHeight: CGFloat = 0.0
    /// 顶部类似于导航栏的视图容器
   // @IBOutlet weak var containerView: UIView!

    var newsModel: NewsModel?
    // 充当tableView的数据源
    var newsModelArr: [NewsModel]?
    
    let newsListProvider = NewsListProvider()
    var newsSpecialModel: NewsSpecialModel? {
        didSet {
            self.setupTableView()
            newsListProvider.newsSpecialModel = newsSpecialModel
        }
    }
//========================================================
// MARK: - 一些方法
//========================================================
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.tableView.dataSource =  self.newsListProvider
        self.newsListProvider.tableView = self.tableView
        self.tableView.delegate = self
        publicMethod.hiddleExtendCellForTableView(self.tableView)
        

        if self.newsModel!.tname == "产品推荐"{
            
            DataTool.loadSpecialNewsDataCPTJ(self.newsModel!.NewsModelid)
            { (newsSpecialModel) -> Void in
                guard newsSpecialModel != nil else {
                    return
                }
                self.newsSpecialModel = newsSpecialModel
                LocalDataTool.saveNewsDetailInfo((self.newsSpecialModel?.id)!, anyModel: newsSpecialModel!)
                self.newsModelArr = self.newsSpecialModel?.infomationlist
            }
            
        }
        if self.newsModel!.tname == "行业资讯"{
          
            DataTool.loadSpecialNewsDataHYZX(self.newsModel!.NewsModelid)
            { (newsSpecialModel) -> Void in
                guard newsSpecialModel != nil else {
                    return
                }
                self.newsSpecialModel = newsSpecialModel
                LocalDataTool.saveNewsDetailInfo((self.newsSpecialModel?.id)!, anyModel: newsSpecialModel!)
                self.newsModelArr = self.newsSpecialModel?.infomationlist
            }

        }
        if self.newsModel!.tname == "职业培训"{
            DataTool.loadSpecialNewsDataXLSH(self.newsModel!.NewsModelid)
            { (newsSpecialModel) -> Void in
                guard newsSpecialModel != nil else {
                    return
                }
                self.newsSpecialModel = newsSpecialModel
                LocalDataTool.saveNewsDetailInfo((self.newsSpecialModel?.id)!, anyModel: newsSpecialModel!)
                self.newsModelArr = self.newsSpecialModel?.infomationlist
                
            }
        }
        
    }

    func setupTableView() {
        
        guard self.newsSpecialModel != nil else {
            return
        }
        
       
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

    }
    
    @IBAction func backBtnClik() {
        self.navigationController?.popViewControllerAnimated(true)
        self.navigationController?.setNavigationBarHidden(false, animated: true)
    }


     // MARK: - SpecialNewsHeardViewDelegate
    func specialNewsTopicBtnClik(index: Int) {

        self.tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: index), atScrollPosition: .Top, animated: true)
    }
    
}

// MARK: - tableView代理
extension SpecialNewsController: UITableViewDelegate, CellAttributeProtocol {
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

        return provideCellHeight((newsSpecialModel?.infomationlist)!, indexPath: indexPath)
        
    }
    
    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        if(cell.respondsToSelector(Selector("setSeparatorInset:"))) {
            cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
        }
        if(cell.respondsToSelector(Selector("setLayoutMargins:"))) {
            cell.layoutMargins = UIEdgeInsetsMake(0, 0, 0, 0)
        }
    }
    
    func tableView(tableView: UITableView,heightForHeaderInSection section: Int) -> CGFloat{
        
        let font = UIFont(name: "Helvetica", size: 16)
        //let topicsHeight: CGFloat
        if self.newsSpecialModel == nil{
            return 0
        }else{
            topicsHeight = publicMethod.getLabHeigh((self.newsSpecialModel!.digest), font:font!, width:SCREEN_WIDTH - 70) + 30
            return topicsHeight
        }
        
    }
    /**
    自定义HeaderSection，达到当前话题的索引颜色
    是红色
    */
    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let sectionView = UIView()
        let titleLabel = UILabel()
        titleLabel.frame = CGRectMake(10, 10, 50, 15)
        titleLabel.text = "摘要"
        
        let topicStr = self.newsSpecialModel?.digest
        
        let digestlabel = UILabel()
        digestlabel.frame = CGRectMake(70, 10, SCREEN_WIDTH - 70, topicsHeight)
        digestlabel.text = topicStr
        
        digestlabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
        
        digestlabel.numberOfLines = 0
        
        sectionView .addSubview(titleLabel)
        
        sectionView .addSubview(digestlabel)
        return sectionView
        

    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        //取消选中
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        
      
        let vc = provideSelectedNewsVc(self.newsModelArr!, indexPath: indexPath)
        self.navigationController?.pushViewController(vc, animated: true)
        if let interactivePopGestureRecognizer = self.navigationController?.interactivePopGestureRecognizer {
            interactivePopGestureRecognizer.delegate = nil
        }
        
    }
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        let sectionHeaderHeight: CGFloat = topicsHeight
        if scrollView.contentOffset.y <= sectionHeaderHeight && scrollView.contentOffset.y >= 0 {
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0)
        }
        else if scrollView.contentOffset.y >= sectionHeaderHeight {
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0)
        }
        
    }
    
}
// 版权属于原作者
// http://code4app.com (cn) http://code4app.net (en)
// 发布代码于最专业的源码分享网站: Code4App.com